package com.shiro.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Collections;

/**
 * CREATE BY Intelij IDEA
 *
 * @author denpeng
 * @Date: 2018/7/1
 * @TIME: 13:33
 * Description: No Description
 */
@Configuration
public class CrosConfig {

    @Bean
    public CorsFilter corsFilter() {

        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration configuration = new CorsConfiguration();

        configuration.setAllowCredentials(true);//应许cooke跨域
        configuration.setAllowedOrigins(Collections.singletonList("*"));//应许什么ip来访问
        configuration.setAllowedHeaders(Collections.singletonList("*"));//应许请求头
        configuration.setAllowedMethods(Collections.singletonList("*"));//应许什么请求方式
        configuration.setMaxAge(300L);//多少时间内不再重复验证

        source.registerCorsConfiguration("/**", configuration);
        return new CorsFilter(source);
    }
}
